package com.luoying.itew.http.rxjava;

import android.accounts.NetworkErrorException;
import android.content.Context;
import android.text.TextUtils;

import com.luoying.itew.event.LoginEvent;
import com.luoying.itew.http.BaseResponse;
import com.luoying.itew.http.HttpCode;

import org.greenrobot.eventbus.EventBus;

import java.net.ConnectException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by lufan on 2019/4/11
 * Class describe
 */
public abstract class BaseHttpObserver<T> implements Observer<BaseResponse<T>> {
    protected Context mContext;

    public BaseHttpObserver() {

    }

    public BaseHttpObserver(Context cxt) {
        this.mContext = cxt;
    }

    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(BaseResponse<T> baseResponse) {
        onRequestEnd();
        String message_common = "网络异常,稍后重试";
        //请求成功
        if (baseResponse.isOk()) {
            try {
                onSuccess(baseResponse);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                if(!TextUtils.isEmpty(baseResponse.getResultMessage())){
                    onFailure(baseResponse.getResultMessage());
                } else {
                    onFailure(message_common);
                }
                if(baseResponse.getResultCode()== HttpCode.ERROR_CODE&&baseResponse.getResultMessage().contains("accesss_token")){
                    //认为用户账号在其他设备登录
                    EventBus.getDefault().post(new LoginEvent(LoginEvent.LOGIN_STATE_SINGLE));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onError(Throwable e) {
        onRequestEnd();
        String msg_timeout = "网络超时或者异常";
        try {
            if (e instanceof ConnectException
                    || e instanceof TimeoutException
                    || e instanceof NetworkErrorException
                    || e instanceof UnknownHostException) {
                onFailure(msg_timeout);
            } else {
                onFailure(e.getMessage());
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    @Override
    public void onComplete() {
        onRequestEnd();
    }

    /**
     * 返回成功
     *
     * @param baseResponse
     */
    protected abstract void onSuccess(BaseResponse<T> baseResponse);

    /**
     * 返回失败
     *
     * @param errorMessage
     */
    protected abstract void onFailure(String errorMessage);

    /**
     * 请求开始
     */
    protected void onRequestStart() {
        showProgressDialog();
    }

    /**
     * 请求结束
     */
    protected void onRequestEnd() {
        closeProgressDialog();
    }

    /**
     * 加载弹窗
     */
    public void showProgressDialog() {

    }

    /**
     * 关闭加载弹窗
     */
    public void closeProgressDialog() {

    }

}
